
/*
 * Copyright 2008 Friendster Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Text;

namespace Friendster.Service.Core
{
    /// <summary>
    /// This class throws appropiate exception
    /// depending on the error defined by Friendster development platform
    /// </summary>
    public sealed class FriendsterException
    {
        private string errorCode = "";
        private string friendster_method = "";
        internal FriendsterException(string fs_method,string code)
        {
            this.errorCode = code;
            this.friendster_method = fs_method;
        }

        /// <summary>
        /// Returns the fully qualified error message for specified error.
        /// </summary>
        public string ErrorMessage
        {
            get
            {
                return Invock_Method();
            }
        }
        
        private string Invock_Method()
        {
            string error = "";
            switch (this.friendster_method)
            {
                case "token":
                    error = Token_Exception();
                    break;
                case "session":
                    error = Session_Exception();
                    break;
                case "user":
                    error = User_Exception();
                    break;
                case "friends":
                    error = Friend_Exception();
                    break;
                case "depth":
                    error = Depth_Exception();
                    break;
                case "arefriends":
                    error = AreFriend_Exception();
                    break;
                case "photo":
                    error = Photo_Exception();
                    break;
                case "photos":
                    error = Photos_Exception();
                    break;
                case "primaryphoto":
                    error = PrimaryPhoto_Exception();
                    break;
                case "shoutout":
                    error = ShoutOut_Exception();
                    break;
                case "widget":
                    error= Widget_Exception();
                    break;
                default:
                    error = "Unknown method exception : " + this.friendster_method;
                    break;
            }
            return error;
        }

        private string Token_Exception()
        {
            return "Unhandled exception: Unknown error occurred.";
        }

        private string Session_Exception()
        {
            return "Unhandled exception: Unknown error occurred.";
        }

        private string User_Exception()
        {
            return "Unhandled exception: Unknown error occurred.";
        }

        private string PrimaryPhoto_Exception()
        {
            return "Unhandled exception: Unknown error occurred.";
        }

        private string Friend_Exception()
        {
            string error = "";
            switch (this.errorCode)
            {
                case "500":
                    error = "Unexpected error: Unexpected internal error.";
                    break;
                case "501":
                    error = "Wrong Method: Invalid HTTP method (must be GET).";
                    break;
                case "403":
                    error = "Invalid session key: Invalid session key\n Or Disabled widget: Widget is disabled and cannot be used \n Or Session expired: The current session has expired.";
                    break;
                case "404":
                    error = "Invalid input parameter: Input parameters missing or invalid \n Or Unknown widget: API Key does not exist.";
                    break;
                case "400":
                    error = "Request replay: The request was already submitted\n Or Invalid signature: Request signature is invalid.";
                    break;
                default:
                    error = "Unhandled exception: Unknown error occurred.";
                    break;
            }
            return error;
        }

        private string Photos_Exception()
        {
            string error = "";
            switch (this.errorCode)
            {
                case "500":
                    error = "Unexpected error: Unexpected internal error.";
                    break;
                case "501":
                    error = "Wrong Method: Invalid HTTP method (must be GET).";
                    break;
                case "403":
                    error = @"Invalid session key: Invalid session key\n Or Disabled widget: Widget is disabled and cannot be used \n Or Session expired: The current session has expired.
                             \n Or Invalid auth token: Auth token is invalid\n Or Expired auth token: Auth token is expired\n ";
                    break;
                case "404":
                    error = "Unknown widget: API Key does not exist.";
                    break;
                case "400":
                    error = "Request replay: The request was already submitted\n Or Invalid signature: Request signature is invalid.";
                    break;
                case "415":
                    error = "Missing or invalid image file: The file uploaded was not an image file.";
                    break;
                default:
                    error = "Unhandled exception: Unknown error occurred.";
                    break;
            }
            return error;
        }

        private string Photo_Exception()
        {
            string error = "";
            switch (this.errorCode)
            {
                case "500":
                    error = "Unexpected error: Unexpected internal error.";
                    break;
                case "501":
                    error = "Wrong Method: Invalid HTTP method (must be GET).";
                    break;
                case "403":
                    error = @"Invalid session key: Invalid session key\n Or Disabled widget: Widget is disabled and cannot be used \n Or Session expired: The current session has expired.
                             \n Or Invalid auth token: Auth token is invalid\n Or Expired auth token: Auth token is expired\n ";
                    break;
                case "404":
                    error = "Unknown widget: API Key does not exist.";
                    break;
                case "400":
                    error = "Request replay: The request was already submitted\n Or Invalid signature: Request signature is invalid.";
                    break;
                case "415":
                    error = "Missing or invalid image file: The file uploaded was not an image file.";
                    break;
                default:
                    error = "Unhandled exception: Unknown error occurred.";
                    break;
            }
            return error;
        }

        private string Depth_Exception()
        {
            string error = "";
            switch (this.errorCode)
            {
                case "500":
                    error = "Unexpected error: Unexpected internal error.";
                    break;
                case "501":
                    error = "Wrong Method: Invalid HTTP method (must be GET).";
                    break;
                case "403":
                    error = "Invalid session key: Invalid session key\n Or Disabled widget: Widget is disabled and cannot be used \n Or Session expired: The current session has expired.";
                    break;
                case "404":
                    error = "Unknown widget: API Key does not exist.";
                    break;
                case "400":
                    error = "Invalid input parameter: Input parameters missing or invalid \n Or Request replay: The request was already submitted\n Or Invalid signature: Request signature is invalid.";
                    break;
                default:
                    error = "Unhandled exception: Unknown error occurred.";
                    break;
            }
            return error;
        }

        private string AreFriend_Exception()
        {
            string error = "";
            switch (this.errorCode)
            {
                case "500":
                    error = "Unexpected error: Unexpected internal error.";
                    break;
                case "501":
                    error = "Wrong Method: Invalid HTTP method (must be GET).";
                    break;
                case "403":
                    error = "Invalid session key: Invalid session key\n Or Disabled widget: Widget is disabled and cannot be used \n Or Session expired: The current session has expired.";
                    break;
                case "404":
                    error = "Unknown widget: API Key does not exist.";
                    break;
                case "400":
                    error = "Invalid input parameter: Input parameters missing or invalid \n Or Request replay: The request was already submitted\n Or Invalid signature: Request signature is invalid.";
                    break;
                default:
                    error = "Unhandled exception: Unknown error occurred.";
                    break;
            }
            return error;
        }

        private string ShoutOut_Exception()
        {
            string error = "";
            switch (this.errorCode)
            {
                case "500":
                    error = "Unexpected error: Unexpected internal error.";
                    break;
                case "501":
                    error = "Wrong Method: Invalid HTTP method (must be GET).";
                    break;
                case "403":
                    error = "Invalid session key: Invalid session key\n Or Disabled widget: Widget is disabled and cannot be used \n Or Session expired: The current session has expired.\n Or Invalid signature: Request signature is invalid";
                    break;
                case "404":
                    error = "Invalid input parameter: Input parameters missing or invalid \n Or Unknown widget: API Key does not exist.";
                    break;
                case "400":
                    error = "Request replay: The request was already submitted.";
                    break;
                default:
                    error = "Unhandled exception: Unknown error occurred.";
                    break;
            }
            return error;
       }

        private string Widget_Exception()
        {
            string error = "";
            switch (this.errorCode)
            {
                case "500":
                    error = "Unexpected error: Unexpected internal error.";
                    break;
                case "501":
                    error = "Wrong Method: Invalid HTTP method (must be GET).";
                    break;
                case "403":
                    error = "Disabled widget: Widget is disabled and cannot be used \n Or Invalid signature: Request signature is invalid \n Or Access denied: object permission do not allow the caller to perform the operation.";
                    break;
                case "404":
                    error = "Invalid input parameter: Input parameters missing or invalid \n Or Unknown widget: API Key does not exist \n Or Unknown object: The requested object does not exist.";
                    break;
                case "400":
                    error = "Request replay: The request was already submitted.";
                    break;
                default:
                    error = "Unhandled exception: Unknown error occurred.";
                    break;
            }
            return error;
        }

    }
}
